package com.example.demo.service.imply;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.annotation.AutoFill;
import com.example.demo.enumeration.OperationType;
import com.example.demo.mapper.ComponentMapper;
import com.example.demo.mapper.InventoryMapper;
import com.example.demo.mapper.StorageMapper;
import com.example.demo.pojo.PublicFields;
import com.example.demo.pojo.dto.StorageDTO;
import com.example.demo.pojo.entity.Component;
import com.example.demo.pojo.entity.Storage;
import com.example.demo.pojo.query.StorageQuery;
import com.example.demo.pojo.vo.ComponentVO;
import com.example.demo.pojo.vo.PageVO;
import com.example.demo.pojo.vo.StorageVO;
import com.example.demo.service.IStorageService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class IStorageServiceImply extends ServiceImpl<StorageMapper, Storage> implements IStorageService {
    private final StorageMapper storageMapper;
    private final ComponentMapper componentMapper;
    private final InventoryMapper inventoryMapper;
    @Override
    @AutoFill(OperationType.INSERT)
    public void addStorage(StorageDTO storageDTO, PublicFields publicFields) {
        Storage storage = new Storage();
        BeanUtils.copyProperties(storageDTO, storage);
        BeanUtils.copyProperties(publicFields, storage);

        baseMapper.insert(storage);
    }

    @Override
    @AutoFill(OperationType.INSERT)
    public void updateStorage(StorageDTO storageDTO, PublicFields publicFields) {
        Storage storage = new Storage();
        BeanUtils.copyProperties(storageDTO, storage);
        BeanUtils.copyProperties(publicFields, storage);

        LambdaUpdateWrapper<Storage> wrapper = new LambdaUpdateWrapper<Storage>()
                .set(StringUtils.hasText(storage.getStorageName()), Storage::getStorageName, storage.getStorageName())
                .set(StringUtils.hasText(storage.getIndexX()), Storage::getIndexX, storage.getIndexX())
                .set(StringUtils.hasText(storage.getIndexY()), Storage::getIndexY, storage.getIndexY())
                .set(StringUtils.hasText(storage.getAddress()), Storage::getAddress, storage.getAddress())
                .set(storage.getRemark() != null, Storage::getRemark, storage.getRemark())
                .set(storage.getStatus() != null, Storage::getStatus, storage.getStatus())
                .set(storage.getUpdateTime() != null, Storage::getUpdateTime, storage.getUpdateTime())
                .eq(Storage::getStorageId, storage.getStorageId());
        baseMapper.update(wrapper);
    }

    @Override
    public PageVO<StorageVO> queryStoragePage(StorageQuery storageQuery) {
        Page<Storage> p = Page.of(storageQuery.getPageNo(), storageQuery.getPageSize());
        p.addOrder(OrderItem.desc("update_time"));
        Page<Storage> page = lambdaQuery()
                .like(StringUtils.hasText(storageQuery.getStorageName()), Storage::getStorageName, storageQuery.getStorageName())
                .like(StringUtils.hasText(storageQuery.getAddress()), Storage::getAddress, storageQuery.getAddress())
                .page(p);

        PageVO<StorageVO> storageVOs = new PageVO<>();

        storageVOs.setPages(p.getPages());
        storageVOs.setTotal(p.getTotal());

        List<Storage> records = p.getRecords();
        if(CollectionUtils.isEmpty(records)){
            storageVOs.setList(Collections.emptyList());
            return storageVOs;
        }
        List<StorageVO> storageVOList = records.stream().map(storage -> {
            StorageVO storageVO = new StorageVO();
            BeanUtils.copyProperties(storage, storageVO);
            int count=inventoryMapper.selectCountById(storageVO.getStorageId());
            storageVO.setNumber(count);
            return storageVO;
            }).collect(Collectors.toList());
        storageVOs.setList(storageVOList);
        return storageVOs;
    }
}
